
package fmu.pi.model.dao.old;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import fmu.pi.model.bean.Cartao;
import fmu.pi.model.bean.Entidade;


public class CartaoDao extends Dao
{  
    public Integer insert(Entidade entidade)
    {
        PreparedStatement statement = null; 
        Cartao cartao = (Cartao) entidade; 
        Integer result = 0; 
        final String QUERY = "insert into Cartao(nome, codigo, limite)"
                + "values(?, ?, ?);";
        try
        {
            statement = getConnection().prepareStatement(QUERY); 
            statement.setString(1, cartao.getBandeira()); 
            statement.setString(2, cartao.getCodigo()); 
            statement.setFloat(3, cartao.getLimite()); 
            result = statement.executeUpdate();  
        }
        catch(SQLException exception)
        {
            System.err.println("Erro ao inserir cartao no banco de dados!"); 
            exception.printStackTrace(); 
        }
        finally
        {
            try
            {
                if(statement != null)
                    statement.close();
            }
            catch(SQLException exception)
            {
                exception.printStackTrace(); 
            }
        }
        
        return result; 
    }
    
    public Integer update(Entidade entidade)
    {
        PreparedStatement statement = null; 
        Cartao cartao = (Cartao) entidade; 
        Integer result = 0; 
        final String QUERY = "update cartao set nome = ?, codigo = ?, limite = ?"
                + "where idcartao = " +String.valueOf(cartao.getIdCard()); 
        
        try
        {
             
            statement = getConnection().prepareStatement(QUERY);   
            statement.setString(1, cartao.getBandeira()); 
            statement.setString(2, cartao.getCodigo()); 
            statement.setFloat(3, cartao.getLimite()); 
            result = statement.executeUpdate();  
        }
        catch(SQLException exception)
        {
            System.err.println("Erro ao alterar cartao no banco de dados!"); 
            exception.printStackTrace(); 
        }
        finally
        {
            try
            {
                if(statement != null)
                    statement.close();
            }
            catch(SQLException exception)
            {
                exception.printStackTrace(); 
            }
        }
        
        return result; 
    }
    
    public Integer delete(Entidade entidade)
    {
        PreparedStatement statement = null; 
        Cartao cartao = (Cartao) entidade;  
        Integer result = 0; 
        final String QUERY = "delete from cartao where idcartao = "
                +cartao.getIdCard();
        
        try
        {
            statement = getConnection().prepareStatement(QUERY); 
            result = statement.executeUpdate();
        }
        catch(SQLException exception)
        {
            System.err.println("Erro ao apagar cartão do banco de dados!"); 
            exception.printStackTrace(); 
        }
        finally
        {
            try
            {
                if(statement != null)
                    statement.close();
            }
            catch(SQLException exception)
            {
                exception.printStackTrace(); 
            }
        }
        
        return result; 
    }
    
    public List<Entidade> read()
    {
        PreparedStatement statement = null;
        ResultSet resultSet = null; 
        List<Entidade> cards = new ArrayList<Entidade>(); 
        final String QUERY = "select * from cartao order by nome;"; 
        
        try
        {
            statement = getConnection().prepareStatement(QUERY); 
            resultSet = statement.executeQuery(); 
            
            while(resultSet.next())
            {
                Cartao card = new Cartao(); 
                card.setIdCard(resultSet.getInt("idcartao"));
                card.setBandeira(resultSet.getString("nome"));  
                card.setCodigo(resultSet.getString("codigo")); 
                card.setLimite(resultSet.getFloat("limite")); 
                cards.add(card); 
            }
           
        }
        catch(SQLException exception)
        {
            System.err.println("Erro ao recuperar cartões do banco de dados!"); 
            exception.printStackTrace(); 
        }
        finally
        {
            try
            {
                if(statement != null ){
                    statement.close(); 
                    resultSet.close(); 
                }
            }
            catch(SQLException exception)
            {
                exception.printStackTrace(); 
            }
        }
        
        return cards; 
    }
}
